<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>正则表达式克隆示例</title>
    <link rel="stylesheet" href="https://www.unpkg.com/ew-message/dist/ew-message.min.css">
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7f6;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            text-align: center;
        }

        .container {
            background-color: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 80%;
            max-width: 600px;
        }

        h1 {
            color: #4CAF50;
            margin-bottom: 20px;
        }

        .input-group {
            margin: 20px 0;
        }

        .input-group label {
            font-size: 18px;
            margin-right: 10px;
        }

        .input-group input {
            padding: 8px;
            font-size: 16px;
            width: 70%;
            margin-right: 10px;
            border-radius: 4px;
            border: 1px solid #ccc;
        }

        .btn {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        .btn:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 30px;
            font-size: 18px;
            font-weight: bold;
            color: #333;
        }

        .result span {
            color: #4CAF50;
        }

        .code-box {
            background-color: #f0f0f0;
            padding: 15px;
            border-radius: 4px;
            font-family: 'Courier New', monospace;
            word-wrap: break-word;
            white-space: pre-wrap;
            text-align: left;
            margin-top: 20px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>正则表达式克隆示例</h1>
        <p>输入一个正则表达式，点击按钮克隆它，并查看原始和克隆正则表达式的匹配结果。</p>

        <div class="input-group">
            <label for="regexInput">输入正则表达式:</label>
            <input type="text" id="regexInput" placeholder="[a-z]+" value="[a-z]+" />
        </div>

        <div class="input-group">
            <label for="testInput">输入测试文本:</label>
            <input type="text" id="testInput" placeholder="hello world" value="hello world" />
        </div>

        <button class="btn" onclick="cloneAndTestRegex()">克隆并测试</button>

        <div class="result">
            <p>原始正则表达式匹配结果:</p>
            <div class="code-box" id="originalResult"></div>
            <p>克隆正则表达式匹配结果:</p>
            <div class="code-box" id="clonedResult"></div>
        </div>
    </div>

    <script src="https://www.unpkg.com/ew-message/dist/ew-message.min.js"></script>
    <script>
        // cloneRegExp 函数实现：克隆正则表达式
        const cloneRegExp = regExp => new RegExp(regExp.source, regExp.flags);

        // 克隆并测试正则表达式
        function cloneAndTestRegex() {
            const regexInput = document.getElementById("regexInput").value;
            const testInput = document.getElementById("testInput").value;

            try {
                // 创建原始正则表达式
                const originalRegex = new RegExp(regexInput);


                // 克隆正则表达式
                const clonedRegex = cloneRegExp(originalRegex);

                // 获取匹配结果
                const originalMatches = testInput.match(originalRegex);
                const clonedMatches = testInput.match(clonedRegex);

                // 显示匹配结果
                document.getElementById("originalResult").textContent = originalMatches ? originalMatches.join(", ") : "无匹配项";
                document.getElementById("clonedResult").textContent = clonedMatches ? clonedMatches.join(", ") : "无匹配项";
            } catch (error) {
                ewMessage.warning("正则表达式无效，请检查格式！");
            }
        }
    </script>
</body>

</html>